package com.ftg.learn.controller;

import com.ftg.learn.service.OssUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/api/oss")
public class OssController {
    @Autowired    private OssUploadService ossUploadService;
    /**
     * 图片上传接口
     * 前端通过表单或FormData提交图片文件
     */
    @PostMapping("/image")
    public Map<String, Object> uploadImage(@RequestParam("file") MultipartFile file) {
        try {
            // 直接调用Service层已测试的方法
            return ossUploadService.uploadImage(file);
        } catch (Exception e) {
            // 异常处理：返回失败信息（与Service层结果格式保持一致）
            return Map.of("success", false, "message", "图片上传失败：" + e.getMessage());
        }
    }
    /**
     * 音频上传接口
     */
    @PostMapping("/audio")
    public Map<String, Object> uploadAudio(@RequestParam("file") MultipartFile file) {
        try {
            return ossUploadService.uploadAudio(file);
        } catch (Exception e) {
            return Map.of("success", false, "message", "音频上传失败：" + e.getMessage());
        }
    }
}
